home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap05 / SineWave / SineWave.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  2.8 KB  |  93 lines

  1. /*-----------------------------------------
  2.    SINEWAVE.C -- Sine Wave Using Polyline
  3.                  (c) Charles Petzold, 1998
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <math.h>
  8.  
  9. #define NUM    1000
  10. #define TWOPI  (2 * 3.14159)
  11.  
  12. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  13.  
  14. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  15.                     PSTR szCmdLine, int iCmdShow)
  16. {
  17.      static TCHAR szAppName[] = TEXT ("SineWave") ;
  18.      HWND         hwnd ;
  19.      MSG          msg ;
  20.      WNDCLASS     wndclass ;
  21.      
  22.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.      wndclass.lpfnWndProc   = WndProc ;
  24.      wndclass.cbClsExtra    = 0 ;
  25.      wndclass.cbWndExtra    = 0 ;
  26.      wndclass.hInstance     = hInstance ;
  27.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  28.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  30.      wndclass.lpszMenuName  = NULL ;
  31.      wndclass.lpszClassName = szAppName ;
  32.           
  33.      if (!RegisterClass (&wndclass))
  34.      {
  35.           MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
  36.                       szAppName, MB_ICONERROR) ;
  37.           return 0 ;
  38.      }
  39.      
  40.      hwnd = CreateWindow (szAppName, TEXT ("Sine Wave Using Polyline"),
  41.                           WS_OVERLAPPEDWINDOW,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT,
  43.                           CW_USEDEFAULT, CW_USEDEFAULT,
  44.                           NULL, NULL, hInstance, NULL) ;
  45.      
  46.      ShowWindow (hwnd, iCmdShow) ;
  47.      UpdateWindow (hwnd) ;
  48.      
  49.      while (GetMessage (&msg, NULL, 0, 0))
  50.      {
  51.           TranslateMessage (&msg) ;
  52.           DispatchMessage (&msg) ;
  53.      }
  54.      return msg.wParam ;
  55. }
  56.  
  57. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  58. {
  59.      static int  cxClient, cyClient ;
  60.      HDC         hdc ;
  61.      int         i ;
  62.      PAINTSTRUCT ps ;
  63.      POINT       apt [NUM] ;
  64.      
  65.      switch (message)
  66.      {
  67.      case WM_SIZE:
  68.           cxClient = LOWORD (lParam) ;
  69.           cyClient = HIWORD (lParam) ;
  70.           return 0 ;
  71.           
  72.      case WM_PAINT:
  73.           hdc = BeginPaint (hwnd, &ps) ;
  74.           
  75.           MoveToEx (hdc, 0,        cyClient / 2, NULL) ;
  76.           LineTo   (hdc, cxClient, cyClient / 2) ;
  77.           
  78.           for (i = 0 ; i < NUM ; i++)
  79.           {
  80.                apt[i].x = i * cxClient / NUM ;
  81.                apt[i].y = (int) (cyClient / 2 * (1 - sin (TWOPI * i / NUM))) ;
  82.           }
  83.           
  84.           Polyline (hdc, apt, NUM) ;
  85.           return 0 ;
  86.           
  87.      case WM_DESTROY:
  88.           PostQuitMessage (0) ;
  89.           return 0 ;
  90.      }
  91.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  92. }
  93.